package com.control.sqlitemanager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.control.dao.BaseSqliteDao;
import com.control.model.Cooker;
import com.control.utils.StringUtils;


public final class CookerListManager  extends BaseSqliteDao<Cooker>{
	
	private  Context mContext;
	
	private SQLiteDatabase mysqlite;
	
	public enum Cooker_column{
		cooker,
		ID,
		COOKER_CODE,
		COOKER_NAME,
		COOKER_IMG_NAME,
		COOKER_IMG_URL,
		OVERTIME,
		POWER_LEVEL,
		COOKER_STATUS,
		IP,
		CONTROL_STATUS,
		WORK_TIME,
		HEATING,
		KEEPWARM;
	}
	
	public CookerListManager(Context context) {
		super(context);
		mContext = context;
	}
	
	public void update(ContentValues cv){
		try {
			mysqlite = this.getWritableDatabase();
			mysqlite.update(String.valueOf(Cooker_column.cooker),cv , "id=?", new String[]{String.valueOf(cv.get(String.valueOf(Cooker_column.ID)))});
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(mysqlite!=null){
				mysqlite.close();
			}
		}
	}

	
	public void insert(ContentValues cv){
		try{
			mysqlite = this.getWritableDatabase();
			mysqlite.insert(String.valueOf(Cooker_column.cooker), String.valueOf(Cooker_column.ID), cv);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(mysqlite!=null){
				mysqlite.close();
			}
		}
	}
	
	public List<Cooker>  fetchAllData(Map<String,String> param){
		
		List<Cooker> lists = new ArrayList<Cooker>();
		
		String pageSize = param.get("pageSize");
		
		if(StringUtils.isEmpty(pageSize)){
			pageSize = "5";
		}
		
		
		try{
			mysqlite = this.getReadableDatabase();
			Cursor cursor =	mysqlite.query(String.valueOf(Cooker_column.cooker), 
					new String[]{
						String.valueOf(Cooker_column.ID),
						String.valueOf(Cooker_column.COOKER_CODE),
						String.valueOf(Cooker_column.COOKER_NAME),
						String.valueOf(Cooker_column.COOKER_IMG_URL),
						String.valueOf(Cooker_column.HEATING),
						String.valueOf(Cooker_column.KEEPWARM),
						String.valueOf(Cooker_column.IP)
					}, 
					null, null, null, null, " id asc",pageSize);
			
			Cooker cooker = null;
			while(cursor.moveToNext()){
				String id = cursor.getString(cursor.getColumnIndex(String.valueOf(Cooker_column.ID)));
				String code = cursor.getString(cursor.getColumnIndex(String.valueOf(Cooker_column.COOKER_CODE)));
				String name = cursor.getString(cursor.getColumnIndex(String.valueOf(Cooker_column.COOKER_NAME)));
				String imgUrl = cursor.getString(cursor.getColumnIndex(String.valueOf(Cooker_column.COOKER_IMG_URL)));
				String heating = cursor.getString(cursor.getColumnIndex(String.valueOf(Cooker_column.HEATING)));
				String keepWarm = cursor.getString(cursor.getColumnIndex(String.valueOf(Cooker_column.KEEPWARM)));
				String ip = cursor.getString(cursor.getColumnIndex(String.valueOf(Cooker_column.IP)));
				
				cooker = new Cooker();
				cooker.setId(Integer.parseInt(id));
				cooker.setCookerCode(code);
				cooker.setCookerName(name);
				cooker.setCookerImgUrl(imgUrl);
				cooker.setHeating(Integer.parseInt(heating));
				cooker.setKeepWarm(Integer.parseInt(keepWarm));
				cooker.setIp(ip);
				lists.add(cooker);
				cooker = null;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(mysqlite!=null){
				mysqlite.close();
			}
		}
		return lists;
	}
	
	public void bathInsert(List<ContentValues> cvs){
		try{
			mysqlite = this.getWritableDatabase();
			mysqlite.beginTransaction();
			for(int i=0,l=cvs.size();i<l;i++){
				ContentValues cv = cvs.get(i);
				mysqlite.insert(String.valueOf(Cooker_column.cooker), String.valueOf(Cooker_column.ID), cv);
			}
			mysqlite.setTransactionSuccessful();
			mysqlite.endTransaction();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(mysqlite!=null){
				mysqlite.close();
			}
		}
	}
	
}
